import { defineStore } from 'pinia';

/**
 * 权限管理Store
 * 用于管理系统各种权限设置和提示信息
 */
export const usePermissionStore = defineStore('permission', {
  state: () => ({
    // 文件操作权限
    fileOperations: {
      // 下载权限
      download: {
        enabled: false, // 是否启用下载功能
        disabledMessage: '目前禁止下载', // 禁用时的提示信息
      },
      // 上传权限
      upload: {
        enabled: true, // 是否启用上传功能
        disabledMessage: '您没有上传权限', // 禁用时的提示信息
        maxSize: 50 * 1024 * 1024, // 最大上传大小（50MB）
        allowedTypes: [], // 允许的文件类型，空数组表示不限制
      },
      // 删除权限
      delete: {
        enabled: true, // 是否启用删除功能
        disabledMessage: '您没有删除权限', // 禁用时的提示信息
      },
      // 编辑权限
      edit: {
        enabled: true, // 是否启用编辑功能
        disabledMessage: '您没有编辑权限', // 禁用时的提示信息
      }
    },
    
    // 页面交互权限
    pageInteraction: {
      // 复制粘贴权限
      copyPaste: {
        enabled: true, // 是否允许复制粘贴，默认允许
        disabledMessage: '当前页面禁止复制粘贴', // 禁用时的提示信息
      },
      // 右键菜单权限
      contextMenu: {
        enabled: true, // 是否允许右键菜单，默认允许
        disabledMessage: '当前页面禁止使用右键菜单', // 禁用时的提示信息
      }
    },
    
    // 用户角色权限
    roles: {
      // 默认为普通用户
      current: 'user', // 可选值: 'admin', 'editor', 'user', 'guest'
      permissions: {
        admin: ['download', 'upload', 'delete', 'edit', 'copyPaste', 'contextMenu'],
        editor: ['download', 'upload', 'edit', 'copyPaste', 'contextMenu'],
        user: ['download', 'copyPaste', 'contextMenu'],
        guest: ['copyPaste', 'contextMenu']
      }
    }
  }),
  
  getters: {
    // 是否可以下载文件
    canDownload: (state) => state.fileOperations.download.enabled,
    
    // 下载禁用时的提示信息
    downloadDisabledMessage: (state) => state.fileOperations.download.disabledMessage,
    
    // 是否可以上传文件
    canUpload: (state) => state.fileOperations.upload.enabled,
    
    // 上传禁用时的提示信息
    uploadDisabledMessage: (state) => state.fileOperations.upload.disabledMessage,
    
    // 是否可以删除文件
    canDelete: (state) => state.fileOperations.delete.enabled,
    
    // 删除禁用时的提示信息
    deleteDisabledMessage: (state) => state.fileOperations.delete.disabledMessage,
    
    // 是否可以编辑文件
    canEdit: (state) => state.fileOperations.edit.enabled,
    
    // 编辑禁用时的提示信息
    editDisabledMessage: (state) => state.fileOperations.edit.disabledMessage,
    
    // 是否允许复制粘贴
    canCopyPaste: (state) => state.pageInteraction.copyPaste.enabled,
    
    // 复制粘贴禁用时的提示信息
    copyPasteDisabledMessage: (state) => state.pageInteraction.copyPaste.disabledMessage,
    
    // 是否允许右键菜单
    canUseContextMenu: (state) => state.pageInteraction.contextMenu.enabled,
    
    // 右键菜单禁用时的提示信息
    contextMenuDisabledMessage: (state) => state.pageInteraction.contextMenu.disabledMessage,
    
    // 当前用户角色
    currentRole: (state) => state.roles.current,
    
    // 当前角色的权限列表
    currentPermissions: (state) => state.roles.permissions[state.roles.current] || []
  },
  
  actions: {
    // 设置下载权限
    setDownloadPermission(enabled, message = null) {
      this.fileOperations.download.enabled = enabled;
      if (message !== null) {
        this.fileOperations.download.disabledMessage = message;
      }
    },
    
    // 设置上传权限
    setUploadPermission(enabled, message = null) {
      this.fileOperations.upload.enabled = enabled;
      if (message !== null) {
        this.fileOperations.upload.disabledMessage = message;
      }
    },
    
    // 设置删除权限
    setDeletePermission(enabled, message = null) {
      this.fileOperations.delete.enabled = enabled;
      if (message !== null) {
        this.fileOperations.delete.disabledMessage = message;
      }
    },
    
    // 设置编辑权限
    setEditPermission(enabled, message = null) {
      this.fileOperations.edit.enabled = enabled;
      if (message !== null) {
        this.fileOperations.edit.disabledMessage = message;
      }
    },
    
    // 设置复制粘贴权限
    setCopyPastePermission(enabled, message = null) {
      this.pageInteraction.copyPaste.enabled = enabled;
      if (message !== null) {
        this.pageInteraction.copyPaste.disabledMessage = message;
      }
    },
    
    // 设置右键菜单权限
    setContextMenuPermission(enabled, message = null) {
      this.pageInteraction.contextMenu.enabled = enabled;
      if (message !== null) {
        this.pageInteraction.contextMenu.disabledMessage = message;
      }
    },
    
    // 设置当前用户角色
    setUserRole(role) {
      if (this.roles.permissions[role]) {
        this.roles.current = role;
        return true;
      }
      return false;
    },
    
    // 根据角色更新权限
    updatePermissionsByRole() {
      const permissions = this.roles.permissions[this.roles.current] || [];
      
      // 更新各项权限
      this.fileOperations.download.enabled = permissions.includes('download');
      this.fileOperations.upload.enabled = permissions.includes('upload');
      this.fileOperations.delete.enabled = permissions.includes('delete');
      this.fileOperations.edit.enabled = permissions.includes('edit');
      this.pageInteraction.copyPaste.enabled = permissions.includes('copyPaste');
      this.pageInteraction.contextMenu.enabled = permissions.includes('contextMenu');
    },
    
    // 重置所有权限为默认值
    resetPermissions() {
      this.$reset();
    }
  },
  
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'edu-permissions',
        storage: localStorage,
        paths: [
          'fileOperations',
          'pageInteraction',
          'roles'
        ]
      }
    ]
  }
}); 